package com.group.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbBillLogMapper;
import com.group.dao.TbBillMapper;
import com.group.dao.TbContractMapper;
import com.group.dao.TbRepairMapper;
import com.group.pojo.Common;
import com.group.pojo.TbBill;
import com.group.pojo.TbBillLog;
import com.group.pojo.TbContract;
import com.group.pojo.TbRepair;
import com.group.pojo.TbRepairExample;
import com.group.pojo.TbRepairExample.Criteria;
import com.group.pojo.TbUser;
import com.group.service.CommonService;
import com.group.service.RepairService;
import com.group.util.CommonUtil;

@Service
public class RepairServiceImpl implements RepairService {
	@Autowired
	private TbRepairMapper repairMapper;
	@Autowired
	private TbBillMapper billMapper;
	@Autowired
	private TbContractMapper contractMapper;
	@Autowired
	private CommonService commonService;
	@Autowired
	private TbBillLogMapper logMapper;

	// 抢单提交 生成维修单。维修表状态：  1 未完成，  2 已完成
	// 更新bill表中的状态，0:审核拒绝，1待审核，2审核通过，待提交维修地址，3维修地址已提交，待维修师抢单，4维修师已抢单，待维修，5维修师已完成，待评价 6维修已完成
	@Override
	public Map<String, String> grabSave(TbRepair repair, HttpServletRequest request) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		String nextId = commonService.getTbCurrentDateNextId(new Common("tb_repair", CommonUtil.getCurrentDate("yyyyMMdd")));
		repair.setId(nextId);

		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");
		repair.setRepairUserId(loginUser.getId());
		repair.setCreateTime(new Date());
		repair.setRepairStatus("1");
		
		int insert = repairMapper.insert(repair);
		
		if (insert == 1) {
			//报修单状态更新
			TbBill bill = new TbBill();
			bill.setId(repair.getBillId());
			bill.setBillStatus("4");
			int update = billMapper.updateByPrimaryKeySelective(bill);
			if (update == 1) {
				
				//报修日志
				TbBillLog log = new TbBillLog();
				String logNextId = commonService.getTbCurrentDateNextId(new Common("tb_bill_log", CommonUtil.getCurrentDate("yyyyMMdd")));
				log.setId(logNextId);
				log.setBillId(repair.getBillId());
				log.setOperateEvent("维修师抢单");
				log.setOperateUserId(loginUser.getId());
				log.setOperateUserType("2");// 工作人员
				log.setOperateTime(new Date());
				int insert2 = logMapper.insert(log);
				if (insert2 == 1) {
					returnMap.put("success", "true");
				}
			}
		}
		return returnMap;
	}

	// 维修完成提交 更新维修单。维修表状态：  1   2 已完成
	// 更新bill表中的状态，0:审核拒绝，1待审核，2审核通过，待提交维修地址，3维修地址已提交，待维修师抢单，4维修师已抢单，待维修，5维修师已完成，待评价 6维修已完成
	// bill_log日志表中插入记录
	@Override
	public Map<String, String> taskSave(TbRepair repair, HttpServletRequest request) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");

		TbRepairExample example = new TbRepairExample();
		example.createCriteria().andBillIdEqualTo(repair.getBillId()).andRepairUserIdEqualTo(loginUser.getId());
		
		List<TbRepair> list = repairMapper.selectByExample(example);
		if (list.size() != 1) {
			throw new RuntimeException("维修任务完成时查找所对应维修单未找到或不止一条！");
		}
		String repairId = list.get(0).getId();
		TbRepair updateRepair = new TbRepair();
		updateRepair.setId(repairId);
		updateRepair.setRepairStatus("2");
		updateRepair.setUpdateTime(new Date());
		
		
		int update = repairMapper.updateByPrimaryKeySelective(updateRepair);

		if (update == 1) {
			//更新bill状态
			TbBill bill = new TbBill();
			bill.setId(repair.getBillId());
			TbBill billQuery = billMapper.selectByPrimaryKey(repair.getBillId());
			String contractId = billQuery.getContractId();
			bill.setBillStatus("5");
			int updateBill = billMapper.updateByPrimaryKeySelective(bill);
			
			if (updateBill == 1) {
				
				TbBillLog log = new TbBillLog();
				String logNextId = commonService.getTbCurrentDateNextId(new Common("tb_bill_log", CommonUtil.getCurrentDate("yyyyMMdd")));
				log.setId(logNextId);
				log.setBillId(repair.getBillId());
				log.setOperateEvent("维修已完成");
				log.setOperateUserId(loginUser.getId());
				log.setOperateUserType("2");// 工作人员
				log.setOperateTime(new Date());
				int insert = logMapper.insert(log);
				if (insert == 1) {
					
					//此次维修已完成，如果合同报修时间未截止，更新为可报修状态
					TbContract contract = new TbContract();
					contract.setId(contractId);
					TbContract con = contractMapper.selectByPrimaryKey(contractId);
					if (con.getEndTime().after(new Date())) {
						contract.setContractStatus("1");//可再次报修
					} else {
						contract.setContractStatus("0");//已过保修期
					}
					int updateContract = contractMapper.updateByPrimaryKeySelective(contract);
					if (updateContract == 1) {
						
						returnMap.put("success", "true");
					}
				}
			}
		}
		return returnMap;
	}

	@Override
	public List<TbBill> taskQuery(HttpServletRequest request) {
		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");
		List<TbBill> list = billMapper.repairTaskQuery(loginUser.getId());
		return list;
	}

	@Override
	public List<TbBill> viewQuery(HttpServletRequest request) {
		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");
		List<TbBill> list = billMapper.repairViewQuery(loginUser.getId());
		return list;
	}

	@Override
	public List<TbRepair> query(TbRepair repair) {
		TbRepairExample example = new TbRepairExample();
		
		Criteria criteria = example.createCriteria();
		
		String billId = repair.getBillId();
		if (billId != null && !"".equals(billId)) {
			criteria.andBillIdEqualTo(billId);
		}
		
		String id = repair.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String repairStatus = repair.getRepairStatus();
		if (repairStatus != null && !"".equals(repairStatus)) {
			criteria.andRepairStatusEqualTo(repairStatus);
		}
		
		String repairUserId = repair.getRepairUserId();
		if (repairUserId != null && !"".equals(repairUserId)) {
			criteria.andRepairUserIdEqualTo(repairUserId);
		}
		
		List<TbRepair> list = repairMapper.selectByExample(example);
		return list;
	}


}
